package com.tware.anexter;


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class mp3 extends Activity {
	private Button bPass;
	private Button bFail;
	private Button bNa;
	private String LOG;
	private Button bPlay;

	private MediaPlayer mPlayer = null;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mp3);
        this.setTitle("aNexter - MP3/MP4 耳机 测试");

        bPass = (Button)findViewById(R.id.btn_pass);
        bPass.setEnabled(false);
        bPass.setOnClickListener(new OnClickListener(){
        	@Override
			public void onClick(View v)
        	{
        		Intent i = new Intent();
        		i.putExtra("LOG", LOG + "PASS|");
//        		i.setClass(mp3.this , brightness.class);  //2012-04-03: removed
        		i.setClass(mp3.this , recording.class);
        		startActivity(i);
        		if (bPlay.getText().equals("停止播放") || 
        			bPlay.getText().equals("开始播放"))
        		{
        			mPlayer.stop(); mPlayer.release();
        		}
        		mp3.this.finish();
        	}
        });
        
        LOG = this.getIntent().getStringExtra("LOG");
        Toast.makeText(getApplicationContext(), LOG, Toast.LENGTH_SHORT).show();
        
        bFail = (Button)findViewById(R.id.btn_fail);
        bFail.setEnabled(false);
        bFail.setOnClickListener(new OnClickListener(){
        	@Override
    		public void onClick(View v)
        	{
        		Intent i = new Intent();
        		i.putExtra("LOG", LOG + "FAIL|");
        		i.setClass(mp3.this , getResults.class);
        		startActivity(i);
        		if (bPlay.getText().equals("停止播放") || 
            			bPlay.getText().equals("开始播放"))
            		{
            			mPlayer.stop(); mPlayer.release();
            		}
        		mp3.this.finish();
        	}
        });
        
        bNa = (Button)findViewById(R.id.btn_na);
        bNa.setVisibility(View.INVISIBLE);
        
        bPlay = (Button)findViewById(R.id.btn_play);
        bPlay.setOnClickListener(new OnClickListener(){
        	@Override
    		public void onClick(View v)
        	{
				if (mPlayer == null)
				{
					mPlayer=MediaPlayer.create(mp3.this, R.raw.lr);
				}
        		if (bPlay.getText().equals("停止播放"))
                	{ bPlay.setText("开始播放"); mPlayer.pause(); return ;}     			
                bPlay.setText("停止播放");
                try {
//                		mPlayer.reset();
						Toast.makeText(getApplicationContext(), 
								"正在播放...", 
								Toast.LENGTH_SHORT).show();

						if (!mPlayer.isPlaying())
						{
							mPlayer.setLooping(true);
							mPlayer.start();
						}
//						mPlayer.start();
						bPass.setEnabled(true);    bFail.setEnabled(true);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					}

        	}
        });	
    }
 
    @Override
    public void onPause()
    {
    	super.onPause();
    	Log.e("MP3", "Do nothing of onPause");
    }

    @Override
    public void onResume()
    {
    	super.onResume();
    	if (mPlayer!=null && !mPlayer.isPlaying()){
    		mPlayer.start();
    	}
    }
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}